package structure;

/**
 * 通过数组实现栈
 */
public class ArrayStatck {

    private Object[] data;
    private int top;
    private int maxSize;

    public ArrayStatck(int maxSize) {
        this.maxSize = maxSize;
        this.top = -1;
        data = new Object[maxSize];
    }

    /**
     * 入栈
     * @param value
     */
    public void push(Object value) throws Exception {
        if(top == maxSize-1){
            throw new Exception("栈满异常");
        }

        data[++top] = value;
    }

    /**
     * 出栈
     * @throws Exception
     */
    public void pop()throws Exception{
        if(top == -1){
            throw  new Exception("栈空异常");
        }

        Object value = data[top--];
    }

    /**
     * 遍历栈
     */
    public void traverse(){
        while (top != -1){
            System.out.println(""+data[top--]);
        }
    }
}
